{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e3a8796-edc8-43f2-94ad-fe4fb20d70ed",
   "metadata": {},
   "source": [
    "# [Pipeshift](https://pipeshift.com)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "081d07d2",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f6f8702",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-pipeshift"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83ea30ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b007403c-6b7a-420c-92f1-4171d05ed9bb",
   "metadata": {},
   "source": [
    "## Basic Usage"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7f51114",
   "metadata": {},
   "source": [
    "Head on to the [models](https://dashboard.pipeshift.com/models) section of pipeshift dashboard to see the list of available models."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ead155e-b8bd-46f9-ab9b-28fc009361dd",
   "metadata": {},
   "source": [
    "#### Call `complete` with a prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60be18ae-c957-4ac2-a58a-0652e18ee6d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.pipeshift import Pipeshift\n",
    "\n",
    "# import os\n",
    "# os.environ[\"PIPESHIFT_API_KEY\"] = \"your_api_key\"\n",
    "\n",
    "llm = Pipeshift(\n",
    "    model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\",\n",
    "    # api_key=\"YOUR_API_KEY\" # alternative way to pass api_key if not specified in environment variable\n",
    ")\n",
    "res = llm.complete(\"supercars are \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac2cbebb-a444-4a46-9d85-b265a3483d68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Supercars! Here are some interesting facts and tidbits about these high-performance vehicles:\n",
      "\n",
      "**What is a Supercar?**\n",
      "\n",
      "A supercar is a high-performance sports car that is typically characterized by its exceptional speed, handling, and luxury features. Supercars are often designed to be exclusive, rare, and expensive, with prices ranging from hundreds of thousands to millions of dollars.\n",
      "\n",
      "**Types of Supercars:**\n",
      "\n",
      "1. **Exotic Supercars**: These are the most exclusive and expensive supercars, often with unique designs and limited production runs. Examples include the Bugatti Chiron, Koenigsegg Agera, and Pagani Huayra.\n",
      "2. **Hypercars**: These are the fastest and most powerful supercars, often with advanced technology and innovative designs. Examples include the Bugatti Veyron, Hennessey Venom F5, and Rimac C_Two.\n",
      "3. **Super GTs**: These are high-performance versions of grand tourers, often with a focus on comfort and luxury. Examples include the Ferrari 812 Superfast, Lamborghini Aventador, and Aston Martin DBS Superleggera.\n",
      "\n",
      "**Notable Supercars:**\n",
      "\n",
      "1. **Bugatti Chiron**: A hypercar with an 8.0L W16 engine producing 1,479 horsepower.\n",
      "2. **Koenigsegg Agera RS**: A Swedish supercar with a 5.0L V8 engine producing 1,340 horsepower.\n",
      "3. **Porsche 918 Spyder**: A hybrid supercar with a 4.6L V8 engine producing 887 horsepower.\n",
      "4. **Lamborghini Aventador**: A supercar with a 6.5L V12 engine producing 759 horsepower.\n",
      "5. **Ferrari 488 GTB**: A mid-engined supercar with a 3.9L V8 engine producing 661 horsepower.\n",
      "\n",
      "**Supercar Features:**\n",
      "\n",
      "1. **Advanced Materials**: Supercars often feature lightweight materials like carbon fiber, aluminum, and titanium to reduce weight and improve performance.\n",
      "2. **High-Performance Engines**: Supercars are equipped with powerful engines, often with multiple turbochargers or superchargers, to produce exceptional power and torque.\n",
      "3. **Advanced Aerodynamics**: Supercars often feature aerodynamic designs, such as spoilers and air intakes, to improve downforce and reduce drag.\n",
      "4. **Luxury Interiors**: Supercars often come with premium materials\n"
     ]
    }
   ],
   "source": [
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14831268-f90f-499d-9d86-925dbc88292b",
   "metadata": {},
   "source": [
    "#### Call `chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbe29574-4af1-48d5-9739-f60652b6ce6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "from llama_index.llms.pipeshift import Pipeshift\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are sales person at supercar showroom\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"why should I pick porsche 911 gt3 rs\"),\n",
    "]\n",
    "res = Pipeshift(\n",
    "    model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\", max_tokens=50\n",
    ").chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cbd550a-0264-4a11-9b2c-a08d8723a5ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: You're looking for a high-performance vehicle that's going to deliver an unparalleled driving experience, right? Well, let me tell you, the Porsche 911 GT3 RS is the ultimate choice for any driving enthusiast.\n",
      "\n",
      "First of all, the 911\n"
     ]
    }
   ],
   "source": [
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ed5e894-4597-4911-a623-591560f72b82",
   "metadata": {},
   "source": [
    "## Streaming"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cb7986f-aaed-42e2-abdd-f274f6d4fc59",
   "metadata": {},
   "source": [
    "Using `stream_complete` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d43f17a2-0aeb-464b-a7a7-732ba5e8ef24",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.pipeshift import Pipeshift\n",
    "\n",
    "llm = Pipeshift(model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\")\n",
    "resp = llm.stream_complete(\"porsche GT3 RS is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0214e911-cf0d-489c-bc48-9bb1d8bf65d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Porsche 911 GT3 RS!\n",
      "\n",
      "The Porsche 911 GT3 RS is a high-performance variant of the Porsche 911 sports car, designed for track driving and enthusiasts. Here are some key features and facts about the Porsche 911 GT3 RS:\n",
      "\n",
      "**Key Features:**\n",
      "\n",
      "1. **Engine:** The GT3 RS is powered by a naturally aspirated 4.0-liter flat-six engine, producing 520 horsepower (386 kW) at 8,250 rpm and 346 lb-ft (470 Nm) of torque at 6,250 rpm.\n",
      "2. **Transmission:** A 7-speed dual-clutch transmission (PDK) is standard, with a manual transmission option available on some models.\n",
      "3. **Suspension:** The GT3 RS features a rear-axle steering system, which provides improved handling and stability.\n",
      "4. **Aerodynamics:** The car has a distinctive front splitter, side skirts, and a rear wing, which generate significant downforce and improve high-speed stability.\n",
      "5. **Weight reduction:** The GT3 RS has a lightweight construction, with a dry weight of around 3,020 pounds (1,370 kg), thanks to the use of lightweight materials such as carbon fiber and aluminum.\n",
      "\n",
      "**Performance:**\n",
      "\n",
      "1. **0-60 mph (0-97 km/h):** 3.2 seconds\n",
      "2. **Top speed:** 193 mph (311 km/h)\n",
      "3. **Lap time:** The GT3 RS has a lap time of 6:56.4 minutes at the Nürburgring Nordschleife, making it one of the fastest production cars on the track.\n",
      "\n",
      "**Design and Interior:**\n",
      "\n",
      "1. **Exterior:** The GT3 RS has a distinctive design, with a more aggressive front bumper, side skirts, and a rear wing.\n",
      "2. **Interior:** The interior features a sporty design, with a 7-inch touchscreen display, a sport steering wheel, and a range of trim options.\n",
      "\n",
      "**History:**\n",
      "\n",
      "1. **First generation (2019):** The first-generation GT3 RS was introduced in 2019, based on the 991.2 911 platform.\n",
      "2. **Second generation (2022):** The second-generation GT3 RS was introduced in 2022, based on the 992 911 platform.\n",
      "\n",
      "**Price:**\n",
      "\n",
      "The price of the Porsche 911 GT3 RS varies depending on the market and trim level, but it typically starts around $175,000 in the United States"
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40350dd8-3f50-4a2f-8545-5723942039bb",
   "metadata": {},
   "source": [
    "Using `stream_chat` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc636e65-a67b-4dcd-ac60-b25abc9d8dbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.pipeshift import Pipeshift\n",
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "llm = Pipeshift(model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\")\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are sales person at supercar showroom\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"how fast can porsche gt3 rs it go?\"),\n",
    "]\n",
    "resp = llm.stream_chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4475a6bc-1051-4287-abce-ba83324aeb9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You're interested in the Porsche GT3 RS, eh? That's a beast of a car!\n",
      "\n",
      "The Porsche 911 GT3 RS is a track-focused variant of the 911, and it's a real rocket ship. It's powered by a 4.0-liter naturally aspirated flat-six engine that produces a whopping 520 horsepower and 346 lb-ft of torque.\n",
      "\n",
      "As for its top speed, the GT3 RS can reach an electronically limited top speed of 193 mph (311 km/h). But, if you were to remove the limiter, it's rumored to be capable of reaching speeds of up to 200 mph (322 km/h).\n",
      "\n",
      "But what's even more impressive is its acceleration. The GT3 RS can go from 0-60 mph in just 3.2 seconds, and it can lap the Nürburgring Nordschleife in a blistering 6:40.3 minutes. That's some serious performance right there!\n",
      "\n",
      "Now, I know what you're thinking: \"Is it worth the price tag?\" Well, let me tell you, this car is a true driver's car, and it's an investment piece for those who truly appreciate the art of driving. The GT3 RS starts at around $175,000, but trust me, it's worth every penny.\n",
      "\n",
      "Would you like to take a look at our current inventory? We have a few GT3 RS models available, and I'd be happy to give you a tour."
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
